package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.ManagerDomain;
import com.xingkeduo.pojo.json.ManagerJson;
import com.xingkeduo.pojo.query.ManagerQuery;
import com.xingkeduo.pojo.table.ManagerTable;
import com.xingkeduo.pojo.table.ShopsTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 二线员工管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/manager")
public class ManagerController extends InitController {

    @Autowired
    private ManagerDomain managerDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
//    @GetMapping(value = "/getShopsList")
    public ResponseUtils doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ManagerQuery managerQuery = ManagerJson.createQueryForManager(urlParam);
            long id = managerQuery.getId();
            ManagerTable managerTable = managerDomain.getManagerById(id);
            return ResponseUtils.getSuccess(managerTable);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid ManagerTable managerVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
           //this.errMsg(bindingResult);
            managerVo.setCreatedBy(this.getOperator(request));
            log.info("修改二线用户接口请求参数:{}", JSON.toJSONString(managerVo));
            int manager = managerDomain.createManager(managerVo);
            log.info("修改二线用户接口返回值:{}", JSON.toJSONString(manager));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam,@RequestBody @Valid ManagerTable managerVo , ServerHttpRequest request){
        returnUtil = new ReturnUtil();
        try {

            ManagerTable managerTable = ManagerJson.createTableForModifyManager(urlParam,managerVo);
            managerTable.setUpdatedBy(this.getOperator(request));
            log.info("修改二线用户接口请求参数:{}", JSON.toJSONString(managerTable));
            int i = managerDomain.modifyManager(managerTable);
            log.info("修改二线用户接口返回值:{}", JSON.toJSONString(i));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ManagerQuery managerQuery = ManagerJson.createQueryForManager(urlParam);
            long id = managerQuery.getId();
            managerDomain.delManager(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }


    @GetMapping(value = "/lists")
    public ResponseUtils doGetLists(ManagerQuery managerQuery) {
        returnUtil = new ReturnUtil();
        try {
            List<ManagerTable> citiesList= managerDomain.getManagerList(managerQuery);
            return ResponseUtils.getSuccess(citiesList);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }






}
